import express from "express";
import dbhelper from "./../../lib/dbhelper";
import async from "async";
let adminRouter=express.Router();


adminRouter.get("/index",(request,response,next)=>{
   let menu="select * from `sys_menu` where m_status=1 order by m_orderby";
   let websql="SELECT * FROM `webinfo`";
   // dbhelper.query(websql,[],function(err,res1)
   // {
   //    // console.log(res1);
   //    dbhelper.query(menu,[],function(err,res2)
   //    {
   //       // console.log(res2);
   //       response.render("./admin/index.html",{menu:res2,webinfo:res1})
   //    })
      
   // })
   async.parallel([
      function (callback) {
          dbhelper.query(websql, [], function (err, res) {
              callback(err, res)
          })
      },
      function (callback) {
          dbhelper.query(menu, [], function (err, res) {
              callback(err, res)
          })
      }

  ], function (err, values) {
      response.render("./admin/index.html", {
          menu: values[1],
          webinfo: values[0]
      });
  })
  
  
})
adminRouter.get("/menu",(request,response,next)=>
{
response.render("./admin/menu.html",{});

})
//获取菜单数据
adminRouter.post("/getmenu",(request,response,next)=>
{
   console.log(request.body);
   let page=request.body.page;
   let limit=request.body.limit;
   let pagecurrent=(page-1)*limit;
   // console.log(pagecurrent,pagenext)
   let querymenusql=`SELECT * FROM sys_menu ORDER BY m_orderby  LIMIT ${pagecurrent},${limit}`;
   let searchmenusql="SELECT * FROM `sys_menu` WHERE m_id=?";
   let count="SELECT COUNT(*) AS num FROM `sys_menu`"
   let num=0;
   dbhelper.query(count,[],(err,result)=>
   {
   console.log(result[0].num);
   num=result[0].num;
   })
   // console.log(count);
   if(!(request.body.key.m_id))
  {
   dbhelper.query(querymenusql,[],(err,result)=>
   {
      // console.log(result.length);
      let obj={
         code: 0,
         msg: "",
         count:num,
         data: result
      }
     if(!err)
     {
        response.json(obj);
     }
   })
  }
  else{
     let params=[request.body.key.m_id]
   dbhelper.query(searchmenusql,params,(err,result)=>
   {
      console.log(result.length);
      let obj={
         code: 0,
         msg: "",
         count:result.length,
         data: result
      }
     if(!err)
     {
        response.json(obj);
     }
   })

     
   //   params=[request.body.]
  }
  

})


//删除的接口
adminRouter.post("/deleteIdmenu",(request,response,next)=>
{
   let deleteIdsql="UPDATE `sys_menu` SET m_status=0 WHERE m_id=?"
// console.log(request.body);
   dbhelper.query(deleteIdsql,[request.body.mId],(err,result)=>
   {
     if(!err&&result.affectedRows>=1)
     {
        response.json({
           msg:"删除成功",
           status:1
        })
     }
     else{
      response.json({
         msg:"删除失败",
         status:0,
         err:err
      })
     }
   })

})


//修改
adminRouter.post("/updateidmenu",(request,response,next)=>
{
   let updateIdsql="UPDATE `sys_menu` SET m_name=?,m_link=?,m_num=?,m_parent=?,m_icon=?,m_url=?,m_orderby=? WHERE m_id=?"
// console.log(request.body);
   let params=[
      request.body.m_name,
      request.body.m_link,
      request.body.m_num,
      request.body.m_parent,
      request.body.m_icon,
      request.body.m_url,
      request.body.m_orderby,
      request.body.m_id,
   ]
   dbhelper.query(updateIdsql,params,(err,result)=>
   {
     if(!err&&result.affectedRows>=1)
     {
        response.json({
           msg:"修改成功",
           status:1
        })
     }
     else{
      response.json({
         msg:"修改失败",
         status:0,
         err:err
      })
     }
   })

})

//修改状态
adminRouter.post("/updateidstatus",(request,response,next)=>
{
   let updateStatussql="UPDATE `sys_menu` SET m_status=? WHERE m_id=?"
// console.log(request.body);
   let params=[
      request.body.sta,
      request.body.mId
   ]
   dbhelper.query(updateStatussql,params,(err,result)=>
   {
     if(!err&&result.affectedRows>=1)
     {
        response.json({
           msg:"修改成功",
           status:1
        })
     }
     else{
      response.json({
         msg:"修改失败",
         status:0
      })
     }
   })

})


//添加
adminRouter.get("/addmenu",(request,response,next)=>
{
response.render("./admin/addmenu.html",{});
})

//添加数据
adminRouter.post("/addmenu",(request,response,next)=>
{
   let addIdsql="INSERT INTO `sys_menu` (`m_name`,`m_link`,`m_num`,`m_status`,`m_parent`,`m_icon`,`m_url`,`m_orderby`) VALUES (?,?,?,?,?,?,?,?)"
// console.log(request.body);
   let params=[
      request.body.m_name,
      request.body.m_link,
      request.body.m_num,
      request.body.m_status,
      request.body.m_parent,
      request.body.m_icon,
      request.body.m_url,
      request.body.m_orderby,
   ]
   dbhelper.query(addIdsql,params,(err,result)=>
   {
     if(!err&&result.affectedRows>=1)
     {
        response.json({
           msg:"添加成功",
           status:1
        })
     }
     else{
      response.json({
         msg:"添加失败",
         status:0,
         err:err
      })
     }
   })

})


//查询
adminRouter.post("/searchmenu",(request,response,next)=>
{
   let searchmenusql="SELECT * FROM `sys_menu` where m_id=?";
   let params=[
      request.body.mId
   ]
   console.log(request.body.mId);
   dbhelper.query(searchmenusql,params,(err,result)=>
   {
      console.log(result)
      if(!err&&result.length>=1)
      {
         response.json(result
         //    {
         //    msg:"查询成功",
         //    status:1
         // }
         )
      }
      else{
       response.json({
          msg:"查询失败",
          status:0,
          err:err
       })
      }
   })

})
module.exports=adminRouter;


